package lenfs.impl;

import lenfs.Server;
import lenfs.util.XDR;
import lenfs.util.constants.RPCConst;

public abstract class ServerImpl {
	protected static final int HEADER_LENGTH = 28;

	public abstract long getProg();

	public abstract long getVersion();

	public abstract void checkPacket(XDR packet, long xid, long proc);

	protected final Server server;

	public ServerImpl(final Server server) {
		this.server = server;
	}

	public static void versionError(final XDR srcPacket, final long xid,
			final int err, final long low, final long high) {
		final XDR reply = new XDR(32, srcPacket.getChannel(),
				srcPacket.getAddress());
		reply.addUInt(xid);
		reply.addUInt(RPCConst.REPLY);
		reply.addUInt(RPCConst.MSG_ACCEPTED);
		reply.addNullAuth();
		reply.addUInt(err);
		reply.addUInt(low);
		reply.addUInt(high);
		reply.send();
	}

	public static void voidError(final XDR srcPacket, final long xid,
			final int err) {
		final XDR reply = new XDR(24, srcPacket.getChannel(),
				srcPacket.getAddress());
		reply.addUInt(xid);
		reply.addUInt(RPCConst.REPLY);
		reply.addUInt(RPCConst.MSG_ACCEPTED);
		reply.addNullAuth();
		reply.addUInt(err);
		reply.send();
	}

	protected static XDR successAnswer(final XDR srcPacket, int length,
			long xid, long status) {
		final XDR reply = new XDR(length + HEADER_LENGTH,
				srcPacket.getChannel(), srcPacket.getAddress());
		reply.addUInt(xid);
		reply.addUInt(RPCConst.REPLY);
		reply.addUInt(RPCConst.MSG_ACCEPTED);
		reply.addNullAuth();
		reply.addUInt(RPCConst.SUCCESS);
		reply.addUInt(status);
		return reply;
	}
}
